package com.bot.nft.module.mb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bot.nft.module.kyc.HttpUtils;
import com.bot.nft.module.mb.domain.MbKyc;
import com.bot.nft.module.mb.domain.MbUserInfo;
import com.bot.nft.module.mb.domain.bo.MbKycBo;
import com.bot.nft.module.mb.domain.vo.MbKycVo;
import com.bot.nft.module.mb.mapper.IMbKycMapper;
import com.bot.nft.module.mb.service.IMbKycService;
import org.apache.catalina.manager.HTMLManagerServlet;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述: 实名认证实现类
 * <p>
 * 时间: 2023-12-01 11:19
 * 负责人: 卢梦丽
 **/
@Service
public class MbKycServiceImpl extends ServiceImpl<IMbKycMapper,MbKyc> implements IMbKycService {

    @Resource
    private IMbKycMapper iMbKycMapper;


    @Override
    public Boolean Kyc(String name, String idNo) {
        Boolean msg = false;
        String msgStr = "";
        String host = "http://dfidveri.market.alicloudapi.com";
        String path = "/verify_id_name";
        String method = "POST";
        String appcode = "a6ec169902184936b257cbc711a485b6";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("id_number", idNo);
        bodys.put("name", name);

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */

            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            String request = EntityUtils.toString(response.getEntity());
            Map map = JSON.parseObject(request, Map.class);
             msgStr = (String) map.get("result_message");

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (msgStr.equals("一致")){
            msg = true;
        }

        return msg;
    }

    @Override
    public Boolean addKyc(MbKycBo mbKycBo) {
        mbKycBo.setId(IdUtil.getSnowflakeNextIdStr());
        mbKycBo.setKycTime(new Date());
        mbKycBo.setStatus(1);
        MbKyc mbKyc = BeanUtil.toBean(mbKycBo, MbKyc.class);
        int insert = iMbKycMapper.insert(mbKyc);
        Boolean msg = false;
        if (insert==1){
            msg = true;
        }

        return msg;
    }

    @Override
    public MbKycVo getMbKyc(String mbId) {
        MbKycVo mbKycVo = iMbKycMapper.getMbKyc(mbId);
        return mbKycVo;
    }

    @Override
    public Boolean updateStatus(String id) {
        Boolean updateStatus = iMbKycMapper.updateStatus(id);
        return updateStatus;
    }
}
